#include "ResourceExtractor.h"



ResourceExtractor::ResourceExtractor(const char* path)
{
	string dir(path);
	if(dir[dir.length()-1] != '/') dir = dir + '/';
/*
	string know_list_names[2] = {"loc.txt",
								 "per.txt"};

	for(int i=0; i<2; i++)
	{
		set<string> know_list;
		gaze_init_set(dir+know_list_names[i],know_list);
		gaze.insert(make_pair(know_list_names[i],know_list));
	}
*/

	string gaze_set_names[30] = {"WikiPeople.lst",
							 "WikiPeopleRedirects.lst",
							 "WikiLocations.lst",
							 "WikiLocationsRedirects.lst",
							 "WikiOrganizations.lst",
							 "WikiOrganizationsRedirects.lst",
							 "WikiSongs.lst",
							 "WikiSongsRedirects.lst",
							 "WikiFilms.lst",
							 "WikiFilmsRedirects.lst",
							 "WikiArtWork.lst",
							 "WikiArtWorkRedirects.lst",
							 "WikiCompetitionsBattlesEvents.lst",
							 "WikiManMadeObjectNames.lst",
							 "WikiManMadeObjectNamesRedirects.lst",
							 "WikiCompetitionsBattlesEventsRedirects.lst",
							 "known_names.big.lst",
							 "known_name.lst",
							 "known_place.lst",
							 "known_jobs.lst",
							 "known_title.lst",
							 "known_country.lst",
							 "known_corporations.lst",
							 "known_state.lst",
							 "currencyFinal.txt",
							 "known_nationalities.lst",
							 "temporal_words.txt",
							 "measurments.txt",
							 "ordinalNumber.txt",
							 "cardinalNumber.txt"};

	for(int i=0; i<30; i++)
	{
		set<string> gaze_set,gaze_set_lower;
		gaze_init_set(dir+gaze_set_names[i],gaze_set,gaze_set_lower);
		gaze.insert(make_pair(gaze_set_names[i],gaze_set));
		gaze_lower.insert(make_pair("LC-"+gaze_set_names[i],gaze_set_lower));
	}

}

void ResourceExtractor::gaze_init_set(const string& path, set<string>& gaze_set, set<string>& gaze_set_lower)
{
	cout << "Initiating from set ... " << path;

	gaze_set.clear();

	ifstream ifs(path.c_str());
	if(!ifs)
	{
		cerr << " : failed." << endl;
		exit(-1);
	}

	string line;
	while(getline(ifs,line))
	{
		line = fs_algo::strip(line);
		if(line.empty()) 
			continue;

		istringstream iss(line);
		string word,entity;

		while(iss >> word) entity += word + " ";
		entity = entity.substr(0,entity.length()-1);

		if(gaze_set.find(entity) == gaze_set.end())
			gaze_set.insert(entity);

		string entity_lower = fs_algo::to_lower(entity);
		if(gaze_set_lower.find(entity_lower) == gaze_set_lower.end())
			gaze_set_lower.insert(entity_lower);
	}

	cout << " : succeed." << endl;
}

void ResourceExtractor::gaze_extract(const vector<string> &seq, vector<vector<string> > &features)
{
	features.clear();
	features.resize(seq.size());

#define DICT_MAX_LEN 10

	for(int l=0; l<(int)seq.size(); l++)
		for(int r=l; r<(int)seq.size() && (r-l+1)<=DICT_MAX_LEN; r++)
		{
			string key = seq[l];
			for(int k=l+1; k<=r; k++) key = key + " " + seq[k];

			for(map<string,set<string> >::iterator it = gaze.begin();
				it != gaze.end();
				it ++)
			{
				if((*it).second.find(key) != (*it).second.end())
				{
					for(int k=l; k<=r; k++)
						gaze_extract_action((*it).first,l,r,k,features[k]);
				}
			}

			string key_lower = fs_algo::to_lower(key);
			for(map<string,set<string> >::iterator it = gaze_lower.begin();
				it != gaze_lower.end();
				it ++)
			{
				if((*it).second.find(key_lower) != (*it).second.end())
				{
					for(int k=l; k<=r; k++)
						gaze_extract_action((*it).first,l,r,k,features[k]);
				}
			}
		}
}

void ResourceExtractor::gaze_extract_action(const string& root, int l, int r, int cur, vector<string>& features)
{
	if(l == r)
	{
		features.push_back(root+"_U");
	}
	else if(l == cur)
	{
		features.push_back(root+"_B");
	}
	else if(cur == r)
	{
		features.push_back(root+"_L");
	}
	else
	{
		features.push_back(root+"_I");
	}
}

///////////////////////////////
